package com.falcon.core.utils;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import cn.hutool.core.util.ReflectUtil;
import com.falcon.core.domain.BasePager;
import com.falcon.core.domain.PagerResponse;
import com.github.pagehelper.PageInfo;

import java.util.Collection;
import java.util.List;
import java.util.Objects;

/**
 * @author zongf
 * @since 2023-05-08
 **/
public class BeanUtils {

    public static <T> T copyProperties(Object source, Class<T> tClass, String... ignoreProperties) {
        return Objects.isNull(source) ? null : BeanUtil.copyProperties(source, tClass, ignoreProperties);
    }
    public static <T> List<T> copyToList(Collection<?> collection, Class<T> targetType) {
        return BeanUtil.copyToList(collection, targetType);
    }

    public static <T> PagerResponse<T> copyToPager(PageInfo pageInfo, Class<T> targetType) {
        PagerResponse<T> pager = new PagerResponse<>();
        pager.setPages(pageInfo.getPages());
        pager.setTotal(((Long)pageInfo.getTotal()).intValue());
        pager.setData(BeanUtils.copyToList(pageInfo.getList(), targetType));
        return pager;
    }

    public static <T> BasePager<T> toPager(PageInfo pageInfo, Class<T> targetType) {
        BasePager<T> pager = new BasePager<>();
        pager.setPages(pageInfo.getPages());
        pager.setTotal(((Long)pageInfo.getTotal()).intValue());
        pager.setItems(BeanUtils.copyToList(pageInfo.getList(), targetType));
        return pager;
    }
    public static <T> BasePager<T> toPager(PageInfo pageInfo) {
        BasePager<T> pager = new BasePager<>();
        pager.setPages(pageInfo.getPages());
        pager.setTotal(((Long)pageInfo.getTotal()).intValue());
        pager.setItems(pageInfo.getList());
        return pager;
    }


    public static <T> T copyIgnoreNullProperties(Object source, Class<T> tClass, String... ignoreProperties) {
        T target = ReflectUtil.newInstanceIfPossible(tClass);
        BeanUtil.copyProperties(source, target, CopyOptions.create().ignoreNullValue());
        return target;
    }


}
